#include "OutputText.h"

#include <QDateTime>

namespace Logger {

OutputText::OutputText(QString fname, int filter) : Output(filter), file(fname) {
	file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Unbuffered | QIODevice::Text);
	out.setDevice(&file);
	out << "\n\n==[ NEW LOG ]==[ " << (QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss")) << " ]==\n\n";
	out.flush();
	file.flush();
}

OutputText::~OutputText() {
	out << "\n--[ END LOG ]--[ " << (QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss")) << " ]--\n\n";
	out.flush();
	file.flush();
	file.close();
}

void OutputText::log(const QString & msg, MsgType type) {
	if ( (type & filter_) != type)
		return;

	out << (typeStr(type)) << " - " << (QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss")) << " - " << msg << "\n";
	out.flush();
	file.flush();
}

QString OutputText::typeStr(MsgType type) {
	switch(type) {
	case mtNone:
		return "     ";
	case mtInfo:
		return "    i";
	case mtWarning:
		return "   .w";
	case mtError:
		return "  .!e";
	case mtCritical:
		return " !!!c";
	default:
		return "     ";
	}
}

} // namespace Logger
